﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;
using RestAPI;
using TopVisionLog;
using TopVisionVideoStream.Models;
using TopVisionVideoStream.TopVisionCenter;

namespace TopVisionVideoStream.Mananger
{
    public class CompanyInfoManager : IDisposable
    {
        public event ResponseResultEventHandler ResponseResultEvent;

        public CompanyInfoManager()
        {
            RegistEvent();
        }

        private void RegistEvent()
        {

        }

        private void UnRegistEvent()
        {

        }


        public void RequestCompanyInfo(string username, string password)
        {
            MQuerycompanyinfo request = new MQuerycompanyinfo(username, password);
            var ret = request.Call((response) =>
            {
                ResponseResultEvent?.Invoke(ResponseType.Manager_Company_querycompanyinfo, response);
            });
        }

        public void RequestMemberStatus(string companyId, string number)
        {
            MGetagentstatus request = new MGetagentstatus(companyId, number);
            var ret = request.Call((response) =>
            {
                ResponseResultEvent?.Invoke(ResponseType.Manager_getagentstatus, response);
            });
        }

        public void RequestMemberStatus(string companyId)
        {
            RequestMemberStatus(companyId, "");
        }

        public async Task<CompanyMembers> RequestMemberStatusAsync(string companyId, string number, string status)
        {
            MGetagentstatus request = new MGetagentstatus(companyId, number);

            CompanyMembers cms = null;

            await Task.Factory.StartNew(async () =>
             {
                 bool responsed = false;

                 int timer = 0;

                 request.Call((response) =>
                 {
                     try
                     {
                         cms = JsonMapper.ToObject<CompanyMembers>(response);

                         responsed = true;
                     }
                     catch (Exception ex)
                     {
                         TVLog.WriteLine(string.Format("[{0}]{1}_{2}_{3}", DateTime.Now, "CompanyInfoManager", "RequestMemberStatusAsync_Task", ex.Message));
                     }
                 });

                 while (!responsed)
                 {
                     await Task.Delay(100);

                     timer += 100;

                     if (timer > 10 * 1000)
                     {
                         break;
                     }
                 }
             });

            return cms;
        }



        #region 个人中心

        public async void TVCRequestMemberStatus(string companyId, string type)
        {
            TVCConferencesUsersModel model = new TVCConferencesUsersModel() { companyid = companyId, type = type };

            TVCenter center = new TVCenter();

            TVCenterResponse result = null;

            await Task.Factory.StartNew(async () =>
             {
                 result = await center.ConferencesUsers(model);
             });

            TimeSpan timer = new TimeSpan();

            TimeSpan delay = TimeSpan.FromMilliseconds(100);

            while (result == null)
            {
                await Task.Delay(delay);

                timer += delay;

                if (timer.TotalSeconds > 30)
                    break;
            }

            if (result == null || result.data == null || !result.success)
            {
                ResponseResultEvent?.Invoke(ResponseType.Manager_getagentstatus_ERR, result == null ? "" : result.msg);
            }
            else
            {
                List<TVCenterResponse_User> users = result.data.users;

                CompanyMembers cms = new CompanyMembers();
                cms.agentinfo = new List<CompanyMemberInfo>();

                for (int i = 0; i < users.Count; i++)
                {
                    CompanyMemberInfo cmi = new CompanyMemberInfo();
                    cmi.realname = users[i].realName;
                    cmi.zwannumber = users[i].zwannumber;
                    cmi.platform = users[i].platform;
                    cmi.status = users[i].status;
                    cms.agentinfo.Add(cmi);
                }

                string json = JsonMapper.ToJson(cms);

                ResponseResultEvent?.Invoke(ResponseType.Manager_getagentstatus, json);
            }
        }

        public async Task<CompanyMembers> TVCRequestMemberStatusAsync(string companyId, string type)
        {
            TVCConferencesUsersModel model = new TVCConferencesUsersModel() { companyid = companyId, type = type };

            TVCenter center = new TVCenter();

            TVCenterResponse result = null;

            await Task.Factory.StartNew(async () =>
            {
                result = await center.ConferencesUsers(model);
            });

            TimeSpan timer = new TimeSpan();

            TimeSpan delay = TimeSpan.FromMilliseconds(100);

            while (result == null)
            {
                await Task.Delay(delay);

                timer += delay;

                if (timer.TotalSeconds > 30)
                    break;
            }

            if (result == null || result.data == null || !result.success)
            {
                return null;
            }
            else
            {
                List<TVCenterResponse_User> users = result.data.users;

                CompanyMembers cms = new CompanyMembers();
                cms.agentinfo = new List<CompanyMemberInfo>();

                for (int i = 0; i < users.Count; i++)
                {
                    CompanyMemberInfo cmi = new CompanyMemberInfo();
                    cmi.realname = users[i].realName;
                    cmi.zwannumber = users[i].zwannumber;
                    cmi.platform = users[i].platform;
                    cmi.status = users[i].status;
                    cms.agentinfo.Add(cmi);
                }

                return cms;
            }
        }


        #endregion


        public void Dispose()
        {
            UnRegistEvent();
        }
    }
}
